<?php

class Admin_UserController extends Zend_Controller_Action
{

    public function init()
    {
        $option = array(
                'layout'=>'layout',
                'layoutPath'=>APPLICATION_PATH .'/layouts/scripts/admin',
        );
        Zend_Layout::startMvc($option);
        $this->model_user = new Application_Model_User();
    }

    public function indexAction()
    {
        $this->view->headTitle('Admin | Danh sách người dùng');
    }

    //show list users
    public function listAction()
    {
        try{
            $keyword = $this->_request->getParam('keyword');
            if($keyword){
                $select = $this->model_user->select()->where("user_fullname like '%$keyword%' or user_email like '%$keyword%'");
            }
            else 
                $select = $this->model_user->Get_All_User();
            $adapter = new Zend_Paginator_Adapter_DbSelect($select);
            $paginator = new Zend_Paginator($adapter);
            $paginator->setItemCountPerPage(10);
            $current = $this->_request->getParam('page', 1);
            $paginator->setCurrentPageNumber($current);
            $this->view->users = $paginator;
            
            $this->_helper->layout()->disableLayout();
        }
        catch (Zend_Exception $ex){
            echo $ex;
        }
    }

    //delete user
    public function deleteAction()
    {
        try{
            $id = $this->_request->getParam('id');
            if($id){
                $row = $this->model_user->find($id)->current();
                if($row)
                    $row->delete();
            }
        }
        catch (Zend_Exception $ex){
            echo $ex;
        }
    }

    //change password user
    public function changpassAction()
    {
        try{
            $id = $this->_request->getParam('id');
            if($id){
                $row = $this->model_user->find($id)->current();
                if($row){
                    $pass = $this->_request->getParam('pass');
                    if($pass){
                        $row->password = md5($pass);
                        $row->save();
                    }                        
                }
                    
            }
            $this->_helper->layout()->disableLayout();
        }
        catch (Zend_Exception $ex){
            echo $ex;
        }
    }

    public function addAction()
    {
        try {
            $form = new Admin_Form_User();
            $this->view->form = $form;
            if($this->_request->isPost()){
                if($form->isValid($this->_request->getPost())){
                    $email = $form->getValue('user_email');
                    $pass = $form->getValue('password');
                    $repass = $form->getValue('repassword');
                    $role = $form->getValue('user_role');
                    $image = $form->getValue('user_image');
                    $name = $form->getValue('user_fullname');
                    $address = $form->getValue('user_address');
                    $gender = $form->getValue('user_gender');
                    $phone = $form->getValue('user_phone');
                    $date = new Zend_Date();
                    
                    
                    if($form->user_image->isUploaded){
                        $form->user_image->receive();
                            
                    }
                    
                    if($pass == $repass){
                        $data = array(
                                'user_email'=>$email,
                                'password'=>md5($pass),
                                'user_role'=>$role,
                                'user_fullname'=>$name,
                                'user_address'=>$address,
                                'user_gender'=>$gender,
                                'user_phone'=>$phone,
                                'user_datecreate'=>$date->now('vi_VN')->setTimezone('Asia/Ho_Chi_Minh')->toString('yyyy-MM-dd HH:mm:ss'),
                                'user_actived'=>1,
                                'user_image'=>$image,     
                        );
                        if($this->model_user->insert($data))
                            $form->reset();
                        
                    }
                    else{
                        $form->repassword->clearErrorMessages();
                        $form->repassword->addError('Mật khẩu nhập lại không đúng');
                    }
                }
            }
        }
        catch (Zend_Exception $ex){
            echo $ex;
        }
    }


}









